package com.wd.jetpack.demo.paging.model;

import androidx.annotation.NonNull;
import androidx.paging.PositionalDataSource;

import com.wd.base.util.Logger;
import com.wd.jetpack.demo.paging.api.RetrofitClient;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * NA<BR>
 *
 * @author W4255
 * @version [V0.0.1, 2021/6/29]
 * @Description: NA
 */
public class MovieDataSource extends PositionalDataSource<Movie> {
    public static final int PER_PAGE = 8;

    /**
     * 首次加载数据
     *
     * @param params
     * @param callback
     */
    @Override
    public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<Movie> callback) {
        if (true) {
            callback.onResult(fetchItems(0, PER_PAGE), 0, 200);
            return;
        }
        RetrofitClient.getInstance().getMovieApi().getMoviesData(0, PER_PAGE)
                .subscribe(new Observer<Movies>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull Movies movies) {
                        Logger.i("loadInitial,onResponse");
                        if (movies == null) {
                            return;
                        }
                        if (callback == null) {
                            return;
                        }
                        callback.onResult(movies.movieList, movies.total);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                        Logger.i("loadInitial,onResponse");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 加载下一页
     *
     * @param params
     * @param callback
     */
    @Override
    public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<Movie> callback) {
        if (false) {
            callback.onResult(fetchItems(params.startPosition, params.loadSize));
            return;
        }
        RetrofitClient.getInstance().getMovieApi().getMoviesData(params.startPosition, PER_PAGE)
                .subscribe(new Observer<Movies>() {
                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull Movies movies) {
                        Logger.i("loadRange,onResponse");
                        if (movies == null) {
                            return;
                        }
                        if (callback == null) {
                            return;
                        }
                        callback.onResult(movies.movieList);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                        Logger.i("loadRange,onError");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private List<Movie> fetchItems(int startPosition, int pageSize) {
        List<Movie> list = new ArrayList<>();
        for (int i = startPosition; i < startPosition + pageSize; i++) {
            Movie concert = new Movie();
            concert.setCover("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201311%2F22%2F172350apmxsexmxjd3sj29.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1627660493&t=81dcd57c5be5b313950f82887efddf62");
            concert.setTitle("连城诀(" + i + ")");
            concert.setContent("主演：吴越，舒畅(" + i + ")");
            list.add(concert);
        }
        return list;
    }
}
